use std::mem::MaybeUninit;

use sqlx::SqlitePool;

use crate::{config::DB_PATH, error::AppErr};

pub mod user;
pub mod device;
pub mod file;
pub mod firm;

static mut POOL: MaybeUninit<SqlitePool> = MaybeUninit::uninit();

pub async fn init() -> Result<(), AppErr> {
    let url = format!("sqlite://{}?mode=rwc", DB_PATH);
    let pool = SqlitePool::connect(&url).await?;
    unsafe {POOL.write(pool);}

    user::init().await?;
    device::init().await?;
    file::init().await?;
    firm::init().await?;

    Ok(())
}


#[inline(always)]
pub fn get_pool() -> &'static SqlitePool {
    unsafe { POOL.assume_init_ref() }
}
